/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.stylefeng.guns.modular.grape.controller;

import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.dictmap.DeptDict;
import cn.stylefeng.guns.modular.grape.entity.Authentication;
import cn.stylefeng.guns.modular.grape.service.AuthenticationService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import static java.util.Objects.nonNull;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.trim;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author wmao
 */
@Controller
@RequestMapping("/config/auth")
public class AuthenticationController extends BaseController {

    @Autowired
    private AuthenticationService authenticationService;

    private String PREFIX = "/modular/grape/config/auth/";

    /**
     * 跳转到添加海关接口
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping("/auth_add")
    public String authAdd() {
        return PREFIX + "auth_add.html";
    }

    /**
     * 新增海关配置
     *
     * @author fengshuonan
     * @param auth
     * @return
     * @Date 2018/12/23 4:57 PM
     */
    @BussinessLog(value = "添加海关接口", key = "simpleName", dict = DeptDict.class)
    @RequestMapping(value = "/saveorupdate")
    @Permission
    @ResponseBody
    public ResponseData addOrUpdate(Authentication auth) {
        if (nonNull(auth)) {
            if (isNotBlank(trim(auth.getId()))) {
                authenticationService.editAuth(auth);
            } else {
                authenticationService.addAuth(auth);
            }
        }
        return SUCCESS_TIP;
    }

    /**
     * 海关接口配置列表
     *
     * @return
     */
    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public ResponseData list() {
        List<Authentication> auths = authenticationService.list(new QueryWrapper<Authentication>().eq("status", 0));
        return isNotEmpty(auths) ? ResponseData.success(auths) : ResponseData.error("找不到记录");
    }

}
